$(function(){
    if($(".video-swiper .swiper-slide").length>4)
    {
        var swiper = new Swiper('.video-swiper', {
            slidesPerView: 4,
            paginationClickable: true,
            spaceBetween: 12,
            initialSlide :1,
            loop:true,
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',
            onSlideChangeEnd:function(swiper)
            {
                swiper.unlockSwipes();
            },
            onSlideChangeStart: function(swiper){
                $(".video-swiper .swiper-slide").removeClass("swiper-no-gray");
                $(".video-swiper .swiper-slide-active").addClass("swiper-no-gray");
                $(".video-swiper .swiper-slide-active").next().addClass("swiper-no-gray");
                $(".video-swiper .swiper-slide-active").next().next().addClass("swiper-no-gray");
                $(".video-swiper .swiper-slide-active").next().next().next().addClass("swiper-no-gray");
                swiper.lockSwipes();

            }
        });
    }
    else {
        $(".video-swiper .swiper-slide").addClass("swiper-less");
    }

    $('.comment').click(function(){$('html,body').animate({scrollTop:$('#commentTextarea').offset().top}, 800);});


    if($(".inner-col").height()>120)
    {
        $('.control').show();
    }

    $('#js-detail').on('click',function(){
        if($(this).parents('.detail').hasClass('active')){
            $(this).parents('.detail').removeClass('active');
        }else{
            $(this).parents('.detail').addClass('active');
        }
    })

    //$(".lv-dt .add-line .fr>.zan").click(function()
    //{
    //    if($(this).find("i").hasClass("cur"))
    //    {
    //        $(this).find("i").removeClass("cur");
    //        $(this).find(".text").text("点赞");
    //        var count=parseInt($(this).find(".num").text());
    //        if(count>0)
    //        {
    //            $(this).find(".num").text(count-1);
    //            common.toast("取消点赞成功！");
    //        }
    //
    //
    //    }
    //    else {
    //        $(this).find("i").addClass("cur");
    //        $(this).find(".text").text("已赞");
    //
    //        var count=parseInt($(this).find(".num").text());
    //
    //        $(this).find(".num").text(count+1);
    //        common.toast("点赞成功！");
    //    }
    //});
});

/**
 * 评论内容
 *
 * @param content_id
 * @returns {boolean}
 */
function comment()
{
    var content_id = $('#content_id').val();
    var comment_text = $('#commentTextarea').val();
    if (comment_text == '') {
        layer.alert('评论内容不能为空');
        return false;
    }
    $.post(
        web_url+'/member/comment/content.html',
        {'content_id':content_id, 'comment_text':comment_text},
        function (res) {
            if (res.code == 200) {
                layer.alert(res.msg);
                location.reload();
                //location.href=web_url+'/content/detail.html?id='+content_id+'#contentcomment';
            } else {

                if (res.status == 0) {
                    location.href=web_url+'/member/sign/login.html';
                }
                else {
                    layer.alert(res.msg);
                }
                //location.reload();
            }
        },'json'
    );
}

//评论点赞
function comment_fav(obj, id)
{
    $.post(
        web_url+'/member/comment/fav.html',
        {'id':id},
        function (res) {
            if (res.code == 200) {
                var num = obj.find('span').text() * 1;
                obj.find('span').html(num + 1);
                obj.find("i").addClass("cur");
            } else if (res.code == 201) {
                var num = obj.find('span').text() * 1;
                obj.find('span').html(num - 1);
                obj.find("i").removeClass("cur");
            } else {
                if (res.status == 0) {
                    location.href=web_url+'/member/sign/login.html';
                } else {
                    layer.alert(res.msg);
                    if (res.status == 0) {
                        setInterval("location.href=web_url+'/member/sign/login.html';",2000);
                    }
                }
            }
        },'json'
    );
}

//收藏
$('#fav_content').click(function(){
    var content_id = $('#content_id').val();
    var num = $('#fav-num').text() * 1 + 1;
    var obj = $(this);
    $.post(
        web_url+'/member/fav/content.html',
        {'content_id':content_id},
        function (res) {
            // if (res.code == 200) {
            //     $('#favNum').html('赞'+res.data.fav_num);
            // }
           // if (res.code == 200) $('#fav-num').html(num);
            //layer.alert(res.msg);
            if (res.code == 201) {
                obj.find("i").removeClass("cur");
            } else if (res.code == 200) {
                obj.find("i").addClass("cur");
            } else {
                if (res.status == 0) location.href=web_url+'/member/sign/login.html';
                else layer.alert(res.msg);
            }
            return false;
        },'json'
    );
});

//点赞
$('#fav2_content').click(function(){
    var content_id = $('#content_id').val();
    var num = $('#fav-num').text() * 1;
    var obj = $(this);
    $.post(
        web_url+'/member/fav/content2.html',
        {'content_id':content_id, 'num':num},
        function (res) {
            if (res.code == 200) {
                $('#fav-num').html(res.data.num);
                obj.find("i").addClass("cur");
                obj.find(".text").text("已赞");
            } else if (res.code == 201) {
                $('#fav-num').html(res.data.num);
                obj.find("i").removeClass("cur");
                obj.find(".text").text("点赞");
            }
            //layer.alert(res.msg);
        },'json'
    );
});


//分享二维码
$(".share-qr-code").mouseover(function()
{
    $(this).find(".qr-code").show();
}).mouseout(function()
{
    $(this).find(".qr-code").hide();
});

/**
 * 加入愿望清单
 */
function wish()
{
    //判断是否登录
    if (member_id == 0) location.href=web_url+'/member/sign/login.html';
    var goods_id = $('#tagDetail').attr('data-id');
    $.post(
        web_url+'/member/wish/goods.html',
        {'goods_id':goods_id},
        function (res) {
            common.toast(res.msg);
            return false;
        },'json'
    );
}

function wish2(goods_id)
{
    //判断是否登录
    if (member_id == 0) location.href=web_url+'/member/sign/login.html';
    $.post(
        web_url+'/member/wish/goods.html',
        {'goods_id':goods_id},
        function (res) {
            alert(res.msg);
            return false;
        },'json'
    );
}

function order()
{
    var goods_id = $('#tagDetail').attr('data-id');
    $.post(
        web_url+'/goods/detail/default.html',
        {'goods_id':goods_id},
        function (res) {
            location.href=res.data.url;
        },'json'
    );
}
